package pingce.share.util;

import java.io.UnsupportedEncodingException;


import ICTCLAS.I3S.AC.ICTCLAS50;

public class ICTSplitter implements Splitter {
	private ICTCLAS50 ic = new ICTCLAS50();
	private static int POS_MAP = 0;
	ICTSplitter(){
		try{
			if(ic.ICTCLAS_Init(".".getBytes("UTF-8")) == false)
				System.out.println("init ict fail!");
			ic.ICTCLAS_SetPOSmap(POS_MAP);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	ICTSplitter(String usrDic){
		try{
			if(ic.ICTCLAS_Init(".".getBytes("UTF-8")) == false)
				System.out.println("init ict fail!");
			ic.ICTCLAS_ImportUserDictFile
			(usrDic.getBytes("UTF-8"), ICTCLAS50.eCodeType.CODE_TYPE_UTF8.ordinal());
//			ic.ICTCLAS_SaveTheUsrDic();
			ic.ICTCLAS_SetPOSmap(POS_MAP);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public String Split(String rawStr, boolean withPOS) {
		// TODO Auto-generated method stub
		try{
			byte[] bts ;
			if(withPOS){
				bts = ic.ICTCLAS_ParagraphProcess(rawStr.getBytes(), 
						ICTCLAS50.eCodeType.CODE_TYPE_UNKNOWN.ordinal(), 1);
			}else{
				bts = ic.ICTCLAS_ParagraphProcess(rawStr.getBytes(), 
						ICTCLAS50.eCodeType.CODE_TYPE_UNKNOWN.ordinal(), 0);
			}
			return new String(bts, 0 , bts.length);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public void release() {
		// TODO Auto-generated method stub
		ic.ICTCLAS_Exit();
	}

}
